<?php

/**
 * Description of MantUsuario
 *
 * @author EaDeveloper
 */
class MantPerfiles extends FastMaintenance {

    public function __construct() {
        $this->table = "kairos_app_perfil";
        $this->gridCols = array("ID", "NAME");
        $this->fieldsConfig = array("ID" => "CODIGO", "NAME" => "PERFIL");
        $this->autoPK = true;
        $this->pkFields = array("ID");
        $this->tittle = "Mantenimiento de Perfiles";
        $this->windowTittle = $this->tittle;
        $this->instanceName = "MantPerfiles";
        $this->db = DbManager::getMe();
    }

    public function includeWidget() {
        $menu = array();
        $pkPerfil = isset($this->currentRecord["ID"]) ? sqlValue($this->currentRecord["ID"], "int") : "0";
        $sqlAcces = "SELECT * FROM (select ID,ORDEN,MENU_PARENT,TITTLE,1 as ESTADO from kairos_app_menu where
                        ID in (select FK_MENU from kairos_app_menu_acces where FK_PERFIL=$pkPerfil)
                        UNION
                       select ID,ORDEN,MENU_PARENT,TITTLE,0 as ESTADO from kairos_app_menu where
                        ID not in (select FK_MENU from kairos_app_menu_acces where FK_PERFIL=$pkPerfil)) opciones order by ID,ORDEN,MENU_PARENT ";

        $menu = $this->db->queryToArray($sqlAcces, true);
        ?>
        <div id="widgetaccesos" style="border: 1px; width: 600px; display: block; " >
            <fieldset>
                <legend>Accesos</legend>
                <table width="100%">
                    <thead>
                        <tr>
                            <th>Opci&oacute;n Menu</th>
                            <th><button type="button" onclick="addAll()" >Todos</button>&nbsp;
                                <button type="button" onclick="remAll()">Ninguno</button></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($menu as $op) {
                            $estado = $op["ESTADO"] == 1 ? 'checked' : '';
                            ?>

                            <tr>
                                <td> <?php echo $op["ORDEN"] != 0 ? "--->" : ""; ?>
            <?php echo $op["TITTLE"]; ?></td>
                                <td align="center">
                                    <input type="checkbox" <?php echo $estado; ?>  name="op-<?php echo $op["ID"] ?>" id="op_<?php echo $op["ID"] ?>" value="<?php echo $op["ESTADO"] ?>" />
                            </tr>
        <?php } ?>
                    </tbody>
                </table>
            </fieldset>
        </div>
        <script type="text/javascript">
            var addAll=function(){
                $("input[name*='op-']").attr("checked","checked");
            }
            var remAll=function(){
                $("input[name*='op-']").attr("checked","");
            }
        </script>
        <?php
    }

    public function saveRec() {
        parent::saveRec();
        $this->saveOptions();
    }

    public function updateRec() {
        parent::updateRec();
        $this->saveOptions();
    }

    public function saveOptions() {
        $pkPer = getParam("ID");
        $this->db->query_delete("kairos_app_menu_acces", "FK_PERFIL=$pkPer");
        reset($_REQUEST);
        while ($arrGET = each($_REQUEST)) {
            $arrExplode = explode("-", $arrGET["key"]);
            if (isset($arrExplode[1])) {
                $tipo = $arrExplode[0];
                if ($tipo == "op") {
                    $this->db->query_insert('kairos_app_menu_acces', array("FK_MENU" => $arrExplode[1], "FK_PERFIL" => $pkPer, "STATUS" => 1));
                }
            }
        }
    }

    public function delRec() {
        $pkPer = getParam("ID");
        $this->db->query_delete("kairos_app_menu_acces", "FK_PERFIL=$pkPer");
        parent::delRec();
    }

}
?>
